package cn.edu.hpu.jie.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.edu.hpu.jie.entity.News;
import cn.edu.hpu.jie.entity.NewsSites;
import cn.edu.hpu.jie.services.NewsService;
import cn.edu.hpu.jie.util.DateUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class NewsAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private String title;
	private String subtitle;
	private String writer;
	private String content;
	private Map<String, Object> session = ActionContext.getContext().getSession();
	private List<News> newslist;
	private int id;
	private NewsSites newssites;
	
	public String insertNews() throws IOException {
		if(!"POST".equals(ServletActionContext.getRequest().getMethod())) {
			return ERROR;
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(title != null && subtitle != null && writer != null && content != null) {
			int loginid = (Integer) session.get("loginid");
			NewsSites ns = new NewsSites();
			
			ns.setTitle(title);
			ns.setWriter(writer);
			ns.setCreatetime(DateUtil.getNow());
			ns.setContent(content);
			ns.setSubtitle(subtitle);
			
			int lines = new NewsService().insertNews(ns, loginid);
			
			out.print(lines);
		} else {
			out.print(2);
		}
		out.flush();
		out.close();
		return null;
	}
	
	public String getNewsList() {
		newslist = new NewsService().getAll();
		if(newslist == null) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String getNewsSites() {
		if(id != 0) {
			newssites = new NewsService().getNewsSitesById(id);
			return SUCCESS;
		}
		return NONE;
	}
	
	public String editNewsSites() throws IOException {
		if(!"POST".equals(ServletActionContext.getRequest().getMethod())) {
			return ERROR;
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		if(title != null && subtitle != null && writer != null && content != null) {
			int loginid = (Integer) session.get("loginid");
			NewsSites ns = new NewsSites();
			
			ns.setId(id);
			ns.setTitle(title);
			ns.setWriter(writer);
			ns.setContent(content);
			ns.setSubtitle(subtitle);
			
			int lines = new NewsService().editNews(ns, loginid);
			
			out.print(lines);
		} else {
			out.print(2);
		}
		out.flush();
		out.close();
		return null;
	}
	
	//set & get
	public void setTitle(String title) {
		this.title = title;
	}
	public void setSubtitle(String subtitle) {
		this.subtitle = subtitle;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	public List<News> getNewslist() {
		return newslist;
	}

	public void setId(int id) {
		this.id = id;
	}

	public NewsSites getNewssites() {
		return newssites;
	}
	
}
